local dulishan = fk.CreateSkill{
  name = "hanqing__dulishan",
  tags = { Skill.Quest },
}

Fk:loadTranslationTable{
  ["hanqing__dulishan"] = "度骊山",
  [":hanqing__dulishan"] = "使命技，出牌阶段，你可以与主公拼点：若你赢，视为发动〖天义〗；没赢，视为响应主公的〖制霸〗。<br>\
  ⬤　斩白蛇 成功·反贼：若你耗尽了最后1点使用【杀】的次数以使用最后一张手牌：【杀】，则亮出你的反贼身份。其他反贼亦可选择亮出，"..
  "该【杀】的伤害基数值改为以此法明置身份的反贼数；若反贼均亮出，你将身份改为“贼首”，增加1点体力上限，回复1点体力。<br>\
  ⬤　就羽翮 成功·忠臣：若你于回合内失去了所有手牌且场上仅有一名手牌最多的角色：主公，则亮出你的忠臣身份。"..
  "主公回复所有体力并可以交给你任意张牌，然后你获得〖洞察〗和〖舍身〗。<br>\
  ⬤　醉因卧 成功·内奸：达成以上某项的发动条件时，你将你的内奸身份牌替换为平民，获得中央区的所有牌并结束回合。",

  ["#hanqing__dulishan"] = "度骊山：和主公拼点，若赢则视为发动“天义”，没赢则视为发动“制霸”",
  ["#hanqing__dulishan-show"] = "度骊山：是否亮出身份，令 %src 使用的【杀】伤害增加？",
  ["#hanqing__dulishan-give"] = "度骊山：你可以交给 %src 任意张牌",
}

dulishan:addEffect("active", {
  anim_type = "control",
  prompt = "#hanqing__dulishan",
  card_num = 0,
  target_num = 1,
  can_use = function(self, player)
    return not player:isKongcheng()
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected, selected_cards)
    return #selected == 0 and to_select.role == "lord" and to_select.role_shown and player:canPindian(to_select)
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    local pindian = player:pindian({target}, dulishan.name)
    if pindian.results[target].winner == player then
      if player.dead then return end
      room:addPlayerMark(player, "hanqing__dulishan_win-turn", 1)
    else
      if target.dead then return end
      local to_get = {}
      local cid = pindian.fromCard and pindian.fromCard:getEffectiveId()
      if room:getCardArea(cid) == Card.DiscardPile then
        table.insert(to_get, cid)
      end
      local toCard = pindian.results[target].toCard
      cid = toCard and toCard:getEffectiveId()
      if room:getCardArea(cid) == Card.DiscardPile then
        table.insertIfNeed(to_get, cid)
      end
      if #to_get > 0 then
        room:obtainCard(target, to_get, true, fk.ReasonJustMove, target, "zhiba")
      end
    end
  end,
})

dulishan:addEffect("targetmod", {
  residue_func = function(self, player, skill, scope, card)
    if player:getMark("hanqing__dulishan_win-turn") > 0 and card and card.trueName == "slash" and scope == Player.HistoryPhase then
      return 1
    end
  end,
  bypass_distances =  function(self, player, skill, card)
    return player:getMark("hanqing__dulishan_win-turn") > 0 and card and card.trueName == "slash"
  end,
  extra_target_func = function(self, player, skill, card)
    if player:getMark("hanqing__dulishan_win-turn") > 0 and card and card.trueName == "slash" then
      return 1
    end
  end,
})

dulishan:addEffect(fk.AfterCardUseDeclared, {
  anim_type = "big",
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(dulishan.name) and data.card.trueName == "slash" and
      (player.role == "rebel" or player.role == "renegade") and
      player.phase == Player.Play and data:isUsingHandcard(player) and
      not data.extraUse and not (data.extra_data or {}).bypass_times and
      player:usedCardTimes("slash", Player.HistoryPhase) ==
      Fk:cloneCard("slash").skill:getMaxUseTime(player, Player.HistoryPhase, Fk:cloneCard("slash"), nil)
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:updateQuestSkillState(player, dulishan.name, false)
    room:invalidateSkill(player, dulishan.name)
    room:setPlayerProperty(player, "role_shown", true)
    room:broadcastProperty(player, "role")
    if player.role == "rebel" then
      local rebels = table.filter(room:getOtherPlayers(player), function (p)
        return p.role == "rebel" and not p.role_shown
      end)
      if #rebels > 0 then
        local req = Request:new(rebels, "AskForChoice")
        req.focus_text = dulishan.name
        req.receive_decode = false
        req.focus_players = room:getOtherPlayers(player, false)
        local dat = {
          {"yes", "no"},
          {"yes", "no"},
          dulishan.name,
          "#hanqing__dulishan-show:"..player.id,
        }
        for _, p in ipairs(rebels) do
          req:setData(p, dat)
          req:setDefaultReply(p, "yes")  --默认项为随机选项
        end
        req:ask()
        for _, p in ipairs(rebels) do
          if req:getResult(p) == "yes" then
            room:setPlayerProperty(p, "role_shown", true)
            room:broadcastProperty(p, "role")
            data.additionalDamage = (data.additionalDamage or 0) + 1
          end
        end
      end
      if not table.find(room.alive_players, function (p)
        return p.role == "rebel" and not p.role_shown
      end) then
        player.role = "rebel_chief"
        room:broadcastProperty(player, "role")
        room:changeMaxHp(player, 1)
        if not player.dead then
          room:recover{
            who = player,
            num = 1,
            recoverBy = player,
            skillName = dulishan.name,
          }
        end
      end
    elseif player.role == "renegade" then
      player.role = "civilian"
      room:broadcastProperty(player, "role")
      if #room:getBanner("@$CenterArea") > 0 then
        room:moveCardTo(room:getBanner("@$CenterArea"), Card.PlayerHand, player, fk.ReasonJustMove, dulishan.name, nil, true, player)
      end
      room.logic:breakTurn()
    end
  end,
})

dulishan:addEffect(fk.AfterCardsMove, {
  anim_type = "big",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(dulishan.name) and player:isKongcheng() and
      player.room.current == player and
      (player.role == "loyalist" or player.role == "renegade") and player.room:getLord() and
      table.every(player.room.alive_players, function (p)
        return player.room:getLord() == p or p:getHandcardNum() < player.room:getLord():getHandcardNum()
      end) then
      for _, move in ipairs(data) do
        if move.from == player then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand then
              return true
            end
          end
        end
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:updateQuestSkillState(player, dulishan.name, false)
    room:invalidateSkill(player, dulishan.name)
    room:setPlayerProperty(player, "role_shown", true)
    room:broadcastProperty(player, "role")
    if player.role == "loyalist" then
      local lord = room:getLord()
      if lord then
        if lord:isWounded() then
          room:recover{
            who = lord,
            num = lord.maxHp - lord.hp,
            recoverBy = player,
            skillName = dulishan.name,
          }
          if player.dead then return end
        end
        if not lord.dead and not lord:isNude() then
          local cards = room:askToCards(lord, {
            min_num = 1,
            max_num = 999,
            include_equip = true,
            skill_name = dulishan.name,
            prompt = "#hanqing__dulishan-give:"..player.id,
            cancelable = true,
          })
          if #cards > 0 then
            room:moveCardTo(cards, Card.PlayerHand, player, fk.ReasonGive, dulishan.name, nil, false, lord)
            if player.dead then return end
          end
        end
        room:handleAddLoseSkills(player, "vd_dongcha|vd_sheshen")
      end
    elseif player.role == "renegade" then
      player.role = "civilian"
      room:broadcastProperty(player, "role")
      if #room:getBanner("@$CenterArea") > 0 then
        room:moveCardTo(room:getBanner("@$CenterArea"), Card.PlayerHand, player, fk.ReasonJustMove, dulishan.name, nil, true, player)
      end
      room.logic:breakTurn()
    end
  end,
})

dulishan:addAcquireEffect(function (self, player, is_start)
  player.room:addSkill("#CenterArea")
end)

return dulishan
